/*
 * Copyright 2020-2024. Huawei Technologies Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

part of '../../huawei_iap.dart';

/// Store user's purchase information about consumables, non-consumables, and subscriptions.
class ConsumePurchaseData {
  /// ID of an app that initiates a purchase.
  int? applicationId;

  /// Indicates whether the subscription is automatically renewed.
  ///
  /// Currently, the value is always `false`.
  bool? autoRenewing;

  /// Order ID on the Huawei IAP server, which uniquely identifies a transaction and is generated by the Huawei IAP server during payment.
  String? orderId;

  /// Software package name of the app that initiates a purchase.
  String? packageName;

  /// Product ID.
  String? productId;

  /// Purchase time.
  int? purchaseTime;

  /// Order status.
  ///
  /// - `-1`: Initialized and invisible
  /// - `0`: Purchased
  /// - `1`: Canceled
  /// - `2`: Refunded
  int? purchaseState;

  /// Reserved information on the merchant side, which is passed by the app during payment.
  String? developerPayload;

  /// Purchase token, which uniquely identifies the mapping between a product and a user.
  ///
  /// It is generated by the Huawei IAP server when the payment is complete.
  String? purchaseToken;

  /// Challenge defined when an app initiates a consumption request, which uniquely identifies the consumption request.
  String? developerChallenge;

  /// Consumption status.
  ///
  /// - `0`: Not consumed
  /// - `1`: Consumed
  int? consumptionState;

  /// Receiving status.
  ///
  /// - `0`: Not received
  /// - `1`: Received
  ///
  /// This parameter is valid only for receiving APIs.
  ///
  /// The value is always `0`. You can ignore this parameter.
  int? acknowledged;

  /// Currency.
  ///
  /// The value must be a currency defined in the ISO 4217 standard, for example, `'USD'`, `'CNY'`, and `'TRY'`.
  String? currency;

  /// Value after the actual price of a product is multiplied by 100.
  ///
  /// The actual price is accurate to two decimal places.
  ///
  /// For example, if the value of this parameter is `501`, the actual product price is `5.01`.
  int? price;

  /// Country or region code of a user service area.
  ///
  /// The value must comply with the ISO 3166 standard, for example: `'US'`, `'CN'`, and `'TR'`.
  String? country;

  /// Response code.
  ///
  /// - `0`: The execution is successful.
  String? responseCode;

  /// Response information.
  String? responseMessage;

  /// Product type.
  ///
  /// - `0`: consumable.
  /// - `1`: non-consumable.
  /// - `2`: renewable subscription.
  /// - `3`: non-renewable subscription.
  int? kind;

  /// Product name.
  String? productName;

  ///Purchase timestamp from 00:00:00 on January 1, 1970 to the purchase time, in millionseconds.
  int? purchaseTimeMillis;

  /// Confirmation.
  int? confirmed;

  /// Purchase type.
  ///
  /// - `0`: in the sandbox
  /// - `1`: in the promotion period (currently unsupported)
  ///
  /// This parameter is not returned during formal purchase.
  int? purchaseType;

  /// Merchant ID, which uniquely identifies a transaction and is generated by the Huawei IAP server during payment.
  String? payOrderId;

  /// Payment method.
  ///
  /// - `'0'`: HUAWEI Points
  /// - `'3'`: Credit card
  /// - `'4'`: Alipay
  /// - `'6'`: Carrier billing
  /// - `'13'`: PayPal
  /// - `'16'`: Debit card
  /// - `'17'`: WeChat Pay
  /// - `'19'`: Gift card
  /// - `'20'`: Balance
  /// - `'21'`: HUAWEI Point card
  /// - `'24'`: WorldPay
  /// - `'31'`: HUAWEI Pay
  /// - `'32'`: Ant Credit Pay
  /// - `'200'`: M-Pesa
  String? payType;

  /// Creates a [ConsumePurchaseData] object.
  ConsumePurchaseData({
    this.autoRenewing,
    this.orderId,
    this.packageName,
    this.applicationId,
    this.productId,
    this.purchaseTime,
    this.purchaseState,
    this.developerPayload,
    this.purchaseToken,
    this.consumptionState,
    this.currency,
    this.price,
    this.country,
    this.developerChallenge,
    this.acknowledged,
    this.responseCode,
    this.responseMessage,
    this.kind,
    this.productName,
    this.purchaseTimeMillis,
    this.confirmed,
    this.purchaseType,
    this.payOrderId,
    this.payType,
  });

  /// Creates a [ConsumePurchaseData] object from a JSON string.
  factory ConsumePurchaseData.fromJson(String str) =>
      ConsumePurchaseData.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory ConsumePurchaseData.fromMap(Map<dynamic, dynamic> json) =>
      ConsumePurchaseData(
        applicationId:
            json['applicationId'] == null ? null : json['applicationId'],
        autoRenewing:
            json['autoRenewing'] == null ? null : json['autoRenewing'],
        orderId: json['orderId'] == null ? null : json['orderId'],
        packageName: json['packageName'] == null ? null : json['packageName'],
        productId: json['productId'] == null ? null : json['productId'],
        purchaseTime:
            json['purchaseTime'] == null ? null : json['purchaseTime'],
        purchaseState:
            json['purchaseState'] == null ? null : json['purchaseState'],
        developerPayload:
            json['developerPayload'] == null ? null : json['developerPayload'],
        purchaseToken:
            json['purchaseToken'] == null ? null : json['purchaseToken'],
        developerChallenge: json['developerChallenge'] == null
            ? null
            : json['developerChallenge'],
        consumptionState:
            json['consumptionState'] == null ? null : json['consumptionState'],
        acknowledged:
            json['acknowledged'] == null ? null : json['acknowledged'],
        currency: json['currency'] == null ? null : json['currency'],
        price: json['price'] == null ? null : json['price'],
        country: json['country'] == null ? null : json['country'],
        responseCode:
            json['responseCode'] == null ? null : json['responseCode'],
        responseMessage:
            json['responseMessage'] == null ? null : json['responseMessage'],
        kind: json['kind'] == null ? null : json['kind'],
        productName: json['productName'] == null ? null : json['productName'],
        purchaseTimeMillis: json['purchaseTimeMillis'] == null
            ? null
            : json['purchaseTimeMillis'],
        confirmed: json['confirmed'] == null ? null : json['confirmed'],
        purchaseType:
            json['purchaseType'] == null ? null : json['purchaseType'],
        payOrderId: json['payOrderId'] == null ? null : json['payOrderId'],
        payType: json['payType'] == null ? null : json['payType'],
      );

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'autoRenewing': autoRenewing,
      'orderId': orderId,
      'packageName': packageName,
      'applicationId': applicationId,
      'productId': productId,
      'purchaseTime': purchaseTime,
      'purchaseState': purchaseState,
      'developerPayload': developerPayload,
      'purchaseToken': purchaseToken,
      'consumptionState': consumptionState,
      'currency': currency,
      'price': price,
      'country': country,
      'developerChallenge': developerChallenge,
      'acknowledged': acknowledged,
      'responseCode': responseCode,
      'responseMessage': responseMessage,
      'kind': kind,
      'productName': productName,
      'purchaseTimeMillis': purchaseTimeMillis,
      'confirmed': confirmed,
      'purchaseType': purchaseType,
      'payOrderId': payOrderId,
      'payType': payType,
    };
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    if (this.runtimeType != other.runtimeType) return false;

    return other is ConsumePurchaseData &&
        this.autoRenewing == other.autoRenewing &&
        this.orderId == other.orderId &&
        this.packageName == other.packageName &&
        this.applicationId == other.applicationId &&
        this.productId == other.productId &&
        this.purchaseTime == other.purchaseTime &&
        this.purchaseState == other.purchaseState &&
        this.developerPayload == other.developerPayload &&
        this.purchaseToken == other.purchaseToken &&
        this.consumptionState == other.consumptionState &&
        this.currency == other.currency &&
        this.price == other.price &&
        this.country == other.country &&
        this.developerChallenge == other.developerChallenge &&
        this.acknowledged == other.acknowledged &&
        this.responseCode == other.responseCode &&
        this.responseMessage == other.responseMessage &&
        this.kind == other.kind &&
        this.productName == other.productName &&
        this.purchaseTimeMillis == other.purchaseTimeMillis &&
        this.confirmed == other.confirmed &&
        this.purchaseType == other.purchaseType &&
        this.payOrderId == other.payOrderId &&
        this.payType == other.payType;
  }

  @override
  int get hashCode => Object.hash(
        autoRenewing,
        orderId,
        packageName,
        applicationId,
        productId,
        purchaseTime,
        purchaseState,
        developerPayload,
        purchaseToken,
        consumptionState,
        currency,
        price,
        country,
        developerChallenge,
        acknowledged,
        responseCode,
        responseMessage,
        kind,
        productName,
        Object.hashAll(
          <Object?>[
            purchaseTimeMillis,
            confirmed,
            purchaseType,
            payOrderId,
            payType,
          ],
        ),
      );
}
